import Vue from 'vue'
import Router from 'vue-router'
// router.app.$store.dispatch('actionName')
// import store from '../store'
Vue.use(Router)


/* Layout */
import Layout from '@/layout'
import { routerListFun, routerListStr } from '@/router/Jurisdiction'
import { deepClone } from '@/utils'
import Vuex from 'vuex'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },





  //-----系统管理




  //微门户


  //-----统计分析



  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true },


]


/**
 * 动态路由
 * @type {({path: string, component: {components: {Navbar: {components: {Hamburger: {methods: {toggleClick(): void}, name: string, props: {isActive: {default: boolean, type: Boolean | BooleanConstructor}}}, Breadcrumb: {data(): {levelList: null}, watch: {$route(): void}, created(): void, methods: {pathCompile(*=): string, handleLink(*): (undefined), isDashboard(*=): boolean, getBreadcrumb(): void}}}, computed: {[p: string]: () => any}, methods: {logout(): Promise<void>, toggleSideBar(): void, logoutAsk(): void}}, AppMain: {computed: {key(): *}, name: string}, Sidebar: {components: {SidebarItem: {components: {Item: {functional: boolean, name: string, render(*, *): [], props: {icon: {default: string, type: String | StringConstructor}, title: {default: string, type: String | StringConstructor}}}, AppLink: {methods: {linkProps(*=): ({rel: string, is: string, href: *, target: string})}, props: {to: {type: StringConstructor, required: boolean}}}}, mixins: [{computed: {device(): *}, methods: {fixBugIniOS(): void}, mounted(): void}], data(): {}, methods: {hasOneShowingChild(*=, *): boolean, resolvePath(*=): (*)}, name: string, props: {item: {type: Object | ObjectConstructor, required: boolean}, isNest: {default: boolean, type: Boolean | BooleanConstructor}, basePath: {default: string, type: String | StringConstructor}}}, Logo: {data(): {logo: string, title: string}, name: string, props: {collapse: {type: Boolean | BooleanConstructor, required: boolean}}}}, computed: {[p: string]: () => any, routes(): *, showLogo(): *, variables(): *, activeMenu(): (default.computed.$route.meta.activeMenu), isCollapse(): *}}}, mixins: [{watch: {$route(*): void}, methods: {$_resizeHandler(): void, $_isMobile(): boolean}, beforeMount(): void, beforeDestroy(): void, mounted(): void}], computed: {fixedHeader(): *, sidebar(): *, classObj(): {hideSidebar, mobile: boolean, withoutAnimation: default.computed.sidebar.withoutAnimation, openSidebar: default.computed.sidebar.opened}, device(): *}, methods: {handleClickOutside(): void}, name: string}, children: [{path: string, component: function(): *, meta: {role: [string, string], icon: string, title: string}, name: string}], id: number}|{redirect: string, path: string, component: {components: {Navbar: {components: {Hamburger: {methods: {toggleClick(): void}, name: string, props: {isActive: {default: boolean, type: Boolean | BooleanConstructor}}}, Breadcrumb: {data(): {levelList: null}, watch: {$route(): void}, created(): void, methods: {pathCompile(*=): string, handleLink(*): (undefined), isDashboard(*=): boolean, getBreadcrumb(): void}}}, computed: {[p: string]: () => any}, methods: {logout(): Promise<void>, toggleSideBar(): void, logoutAsk(): void}}, AppMain: {computed: {key(): *}, name: string}, Sidebar: {components: {SidebarItem: {components: {Item: {functional: boolean, name: string, render(*, *): [], props: {icon: {default: string, type: String | StringConstructor}, title: {default: string, type: String | StringConstructor}}}, AppLink: {methods: {linkProps(*=): ({rel: string, is: string, href: *, target: string})}, props: {to: {type: StringConstructor, required: boolean}}}}, mixins: [{computed: {device(): *}, methods: {fixBugIniOS(): void}, mounted(): void}], data(): {}, methods: {hasOneShowingChild(*=, *): boolean, resolvePath(*=): (*)}, name: string, props: {item: {type: Object | ObjectConstructor, required: boolean}, isNest: {default: boolean, type: Boolean | BooleanConstructor}, basePath: {default: string, type: String | StringConstructor}}}, Logo: {data(): {logo: string, title: string}, name: string, props: {collapse: {type: Boolean | BooleanConstructor, required: boolean}}}}, computed: {[p: string]: () => any, routes(): *, showLogo(): *, variables(): *, activeMenu(): (default.computed.$route.meta.activeMenu), isCollapse(): *}}}, mixins: [{watch: {$route(*): void}, methods: {$_resizeHandler(): void, $_isMobile(): boolean}, beforeMount(): void, beforeDestroy(): void, mounted(): void}], computed: {fixedHeader(): *, sidebar(): *, classObj(): {hideSidebar, mobile: boolean, withoutAnimation: default.computed.sidebar.withoutAnimation, openSidebar: default.computed.sidebar.opened}, device(): *}, methods: {handleClickOutside(): void}, name: string}, children: [{path: string, component: function(): *, meta: {icon: string, title: string}, name: string, id: number}, {path: string, component: function(): *, meta: {icon: string, title: string}, name: string, id: number}], meta: {role: [string, string, string], icon: string, title: string}, id: number}|{path: string, component: {components: {Navbar: {components: {Hamburger: {methods: {toggleClick(): void}, name: string, props: {isActive: {default: boolean, type: Boolean | BooleanConstructor}}}, Breadcrumb: {data(): {levelList: null}, watch: {$route(): void}, created(): void, methods: {pathCompile(*=): string, handleLink(*): (undefined), isDashboard(*=): boolean, getBreadcrumb(): void}}}, computed: {[p: string]: () => any}, methods: {logout(): Promise<void>, toggleSideBar(): void, logoutAsk(): void}}, AppMain: {computed: {key(): *}, name: string}, Sidebar: {components: {SidebarItem: {components: {Item: {functional: boolean, name: string, render(*, *): [], props: {icon: {default: string, type: String | StringConstructor}, title: {default: string, type: String | StringConstructor}}}, AppLink: {methods: {linkProps(*=): ({rel: string, is: string, href: *, target: string})}, props: {to: {type: StringConstructor, required: boolean}}}}, mixins: [{computed: {device(): *}, methods: {fixBugIniOS(): void}, mounted(): void}], data(): {}, methods: {hasOneShowingChild(*=, *): boolean, resolvePath(*=): (*)}, name: string, props: {item: {type: Object | ObjectConstructor, required: boolean}, isNest: {default: boolean, type: Boolean | BooleanConstructor}, basePath: {default: string, type: String | StringConstructor}}}, Logo: {data(): {logo: string, title: string}, name: string, props: {collapse: {type: Boolean | BooleanConstructor, required: boolean}}}}, computed: {[p: string]: () => any, routes(): *, showLogo(): *, variables(): *, activeMenu(): (default.computed.$route.meta.activeMenu), isCollapse(): *}}}, mixins: [{watch: {$route(*): void}, methods: {$_resizeHandler(): void, $_isMobile(): boolean}, beforeMount(): void, beforeDestroy(): void, mounted(): void}], computed: {fixedHeader(): *, sidebar(): *, classObj(): {hideSidebar, mobile: boolean, withoutAnimation: default.computed.sidebar.withoutAnimation, openSidebar: default.computed.sidebar.opened}, device(): *}, methods: {handleClickOutside(): void}, name: string}, children: [{path: string, component: function(): *, meta: {role: [string, string, string], icon: string, title: string}, name: string, id: number}], id: number}|{redirect: string, path: string, component: {components: {Navbar: {components: {Hamburger: {methods: {toggleClick(): void}, name: string, props: {isActive: {default: boolean, type: Boolean | BooleanConstructor}}}, Breadcrumb: {data(): {levelList: null}, watch: {$route(): void}, created(): void, methods: {pathCompile(*=): string, handleLink(*): (undefined), isDashboard(*=): boolean, getBreadcrumb(): void}}}, computed: {[p: string]: () => any}, methods: {logout(): Promise<void>, toggleSideBar(): void, logoutAsk(): void}}, AppMain: {computed: {key(): *}, name: string}, Sidebar: {components: {SidebarItem: {components: {Item: {functional: boolean, name: string, render(*, *): [], props: {icon: {default: string, type: String | StringConstructor}, title: {default: string, type: String | StringConstructor}}}, AppLink: {methods: {linkProps(*=): ({rel: string, is: string, href: *, target: string})}, props: {to: {type: StringConstructor, required: boolean}}}}, mixins: [{computed: {device(): *}, methods: {fixBugIniOS(): void}, mounted(): void}], data(): {}, methods: {hasOneShowingChild(*=, *): boolean, resolvePath(*=): (*)}, name: string, props: {item: {type: Object | ObjectConstructor, required: boolean}, isNest: {default: boolean, type: Boolean | BooleanConstructor}, basePath: {default: string, type: String | StringConstructor}}}, Logo: {data(): {logo: string, title: string}, name: string, props: {collapse: {type: Boolean | BooleanConstructor, required: boolean}}}}, computed: {[p: string]: () => any, routes(): *, showLogo(): *, variables(): *, activeMenu(): (default.computed.$route.meta.activeMenu), isCollapse(): *}}}, mixins: [{watch: {$route(*): void}, methods: {$_resizeHandler(): void, $_isMobile(): boolean}, beforeMount(): void, beforeDestroy(): void, mounted(): void}], computed: {fixedHeader(): *, sidebar(): *, classObj(): {hideSidebar, mobile: boolean, withoutAnimation: default.computed.sidebar.withoutAnimation, openSidebar: default.computed.sidebar.opened}, device(): *}, methods: {handleClickOutside(): void}, name: string}, children: [{path: string, component: function(): *, meta: {roles: [string], icon: string, title: string}, name: string, id: number}, {path: string, component: function(): *, meta: {roles: [string, string], icon: string, title: string}, name: string, id: number}, {redirect: string, path: string, component: function(): *, children: [{path: string, component: function(): *, meta: {icon: string, title: string}, name: string, id: number}, {path: string, component: function(): *, meta: {roles: [string], icon: string, title: string}, name: string, id: number}], meta: {icon: string, title: string}, name: string, id: number}], meta: {icon: string, title: string}, id: number}|{path: string, component: {components: {Navbar: {components: {Hamburger: {methods: {toggleClick(): void}, name: string, props: {isActive: {default: boolean, type: Boolean | BooleanConstructor}}}, Breadcrumb: {data(): {levelList: null}, watch: {$route(): void}, created(): void, methods: {pathCompile(*=): string, handleLink(*): (undefined), isDashboard(*=): boolean, getBreadcrumb(): void}}}, computed: {[p: string]: () => any}, methods: {logout(): Promise<void>, toggleSideBar(): void, logoutAsk(): void}}, AppMain: {computed: {key(): *}, name: string}, Sidebar: {components: {SidebarItem: {components: {Item: {functional: boolean, name: string, render(*, *): [], props: {icon: {default: string, type: String | StringConstructor}, title: {default: string, type: String | StringConstructor}}}, AppLink: {methods: {linkProps(*=): ({rel: string, is: string, href: *, target: string})}, props: {to: {type: StringConstructor, required: boolean}}}}, mixins: [{computed: {device(): *}, methods: {fixBugIniOS(): void}, mounted(): void}], data(): {}, methods: {hasOneShowingChild(*=, *): boolean, resolvePath(*=): (*)}, name: string, props: {item: {type: Object | ObjectConstructor, required: boolean}, isNest: {default: boolean, type: Boolean | BooleanConstructor}, basePath: {default: string, type: String | StringConstructor}}}, Logo: {data(): {logo: string, title: string}, name: string, props: {collapse: {type: Boolean | BooleanConstructor, required: boolean}}}}, computed: {[p: string]: () => any, routes(): *, showLogo(): *, variables(): *, activeMenu(): (default.computed.$route.meta.activeMenu), isCollapse(): *}}}, mixins: [{watch: {$route(*): void}, methods: {$_resizeHandler(): void, $_isMobile(): boolean}, beforeMount(): void, beforeDestroy(): void, mounted(): void}], computed: {fixedHeader(): *, sidebar(): *, classObj(): {hideSidebar, mobile: boolean, withoutAnimation: default.computed.sidebar.withoutAnimation, openSidebar: default.computed.sidebar.opened}, device(): *}, methods: {handleClickOutside(): void}, name: string}, children: [{path: string, component: function(): *, meta: {role: [string, string, string], icon: string, title: string}, name: string, id: number}], id: number})[]}
 */
let dynamicRoutes=[
  {
    id:1,
    path: '/tenant',
    component: Layout,
    // redirect: '/tenant',
    children: [{
      id:1,
      path: '/tenant',
      name: 'Tenant',
      component: () => import('@/views/tenant/index'),
      // component:_import(0,'tenant/index'),
      // meta: { title: 'Dashboard', icon: 'dashboard' }
      meta: { title: '租户管理', icon: 'table' ,role: ['RA0001','RA0002'] }
    }]
  },
  {
    id:2,
    path: '/schoolmgt',
    component: Layout,
    redirect: 'noRedirect',
    meta: { title: '校务管理', icon: 'school', role: ['RA0001','RA0002','RA0003']},
    children: [
      {
        id:21,
        path: '/students',
        name: 'Students',
        component: () => import('@/views/schoolmgt/students'),
        // component:_import(11,'schoolmgt/students'),
        meta: { title: '学生信息', icon: 'students' }
      },
      {
        id:22,
        path: '/teachers',
        name: 'Teachers',
        component: () => import('@/views/schoolmgt/teachers'),
        // component:_import(12,'schoolmgt/teachers'),

        meta: { title: '教师管理', icon: 'teachers' }
      },

      {
        id:23,
        path: 'grades',
        name: 'Grade',
        component: () => import('@/views/schoolmgt/grades'),
        meta: { title: '年级管理', icon: 'grade' }
      },
    ]
  },
  {
    id:3,
    path: '/videomgt',
    component: Layout,
    children: [
      {
        id:3,
        path: 'index',
        name: 'Videomgt',
        component: () => import('@/views/videomgt/index2'),
        // component:_import(21,'videomgt/index2'),

        // path: 'http://ys.vlab2.chinamcloud.com/',
        meta: { title: '视频管理', icon: 'link',role: ['RA0001','RA0002','RA0003'] }
      }
    ]
  },
  {
    id:4,
    path: '/basicmgt',
    component: Layout,
    // redirect: '/basicmgt',
    redirect: 'noRedirect',
    // alwaysShow: true,
    meta: { title: '系统管理', icon: 'nested' },
    children: [
      {
        id:41,
        path: '/base',
        name: 'Base',
        component: () => import('@/views/basicmgt/base/index'),
        meta: { role: ['RA0001'], title: '基础配置', icon: 'tree' }
      },
      {
        id:42,
        path: '/order',
        name: 'Order',
        component: () => import('@/views/basicmgt/order/order'),
        // component:_import(32,'basicmgt/order/order'),

        meta: { role: ['RA0001','RA0002'], title: '套餐设置', icon: 'money' }
      },

      {
        id:43,
        path: 'usermgt',
        name: 'Usermgt',
        redirect: 'noRedirect',
        component: () => import('@/views/basicmgt/usermgt'),
        meta: { title: '用户管理', icon: 'peoples' },
        children: [
          {
            id:431,
            path: '/consumer',
            name: 'Consumer',
            component: () => import('@/views/basicmgt/usermgt/consumer'),
            meta: { title: '用户列表', icon: 'user' }
          },
          {
            id:432,
            path: '/role',
            name: 'Role',
            component: () => import('@/views/permission/role'),
            meta: { title: '角色管理', icon: 'lock', role: ['RA0001'],}
          }

        ]
      }

    ]

  },
  {
    id:5,
    path: '/viewmin',
    component: Layout,
    children: [
      {
        id:5,
        path: 'index',
        name: 'Viewmin',
        component: () => import('@/views/viewmin/index'),
        // component:_import(41,'viewmin/index'),

        //path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: { title: '微门户', icon: 'component',role: ['RA0001','RA0002','RA0003'] }
      }
    ]
  },
  {
    id:6,
    path: '/dataview',
    component: Layout,
    redirect: '/',
    name: 'Dataview',
    // meta: { title: 'dataview', icon: 'example' },
    children: [
      {
        id:6,
        path: 'index',
        component: () => import('@/views/dataview/index'),
        // component:_import(51,'dataview/index'),
        meta: { title: '统计分析', icon: 'example',role: ['RA0001','RA0002','RA0003']}
      },
    ]
  },
]

export const getDynamicRoutes =function(){
  // return JSON.parse(JSON.stringify(dynamicRoutes))
  return dynamicRoutes
}

// let arr=[0,10,11,12,20,21,30,31,32,33,331,332,40,41,50,51]   //权限列表数组
// let arr=[1,11,2,21,22,3,31,4,41,42,43,431,432,5,51,6,61]   //权限列表数组
//
// let dpdynamicRoutes = deepClone(dynamicRoutes)
// let arrRouter= routerListFun(arr,dpdynamicRoutes)
//
// // console.log(JSON.stringify(arrRouter))
// for (let i = 0; i < arrRouter.length; i++) {
//   constantRoutes.push(arrRouter[i])
// }

// console.log("constantRoutes=2="+JSON.stringify(constantRoutes))

const createRouter = () => new Router({
  // mode: 'history', // require service support
  // mode: 'hash', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
  console.log("resetRouter="+JSON.stringify( newRouter.matcher))

}

export default router
